package sparrowGui.item
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import sparrowGui.SparrowGUI;
	import sparrowGui.utils.PubFun;

	/**
	 * 列表和表格用到的类似td的项
	 * @author pelephone
	 */	
	public class LabelItem extends Item
	{
		public var label:TextField;
		
		public function LabelItem(data:Object=null,argSkin:Sprite=null,vars:Object=null)
		{
			vars = vars || SparrowGUI.getIns().labelItemVars;
			label = new TextField();
			super(data,argSkin,vars);
			
			// 跟据文本的宽度重新设置本组件的宽
//			if(!_label.text) return;
//			setSize((_label.textWidth+4),(_label.textHeight+4));
		}
		
		override protected function init():void
		{
			var tf:TextFormat = (new TextFormat());
			tf.align = "center";
			label.defaultTextFormat = tf;
			label.styleSheet = SparrowGUI.txtCss;
			label.mouseEnabled = label.selectable = false;
			label.height = style.height;
			
			addChild(label);
			super.init();
		}
		
		override public function set style(vars:Object):void
		{
			if(!vars) return;
			super.style = vars;
			
			label.width = vars.width;
//			label.height = vars.height;
			label.x = vars.paddingLeft;
			label.y = vars.paddingTop;
		}
		
		override public function setSize(w:Number, h:Number):void
		{
			if(w) label.width = w;
			if(h) label.height = h;
			super.setSize(w,h);
		}
		
		/**
		 * 此参数可以为数字和字符，用于在文本里显示
		 * @param o
		 */		
		override public function update(o:Object):void
		{
			super.update(o);
			text = String(o || "");
		}
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		
		public function get text():String
		{
			return label.text;
		}
		
		public function set text(value:String):void
		{
			label.text = value;
/*			if(!itemTag)
				label.text = value;
			else
				label.htmlText = PubFun.setTextTag(value,itemTag);*/
		}
		
		public function set htmlText(value:String):void
		{
			label.htmlText = value;
		}
		
		/**
		 * Sets/gets the width of the component.
		 */
		override public function set width(w:Number):void
		{
			label.width = w;
			super.width = w;
		}
		
		// 获取长度
		public function get length():int
		{
			return label.textWidth;
		}
	}
}